﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using Telerik.Web.UI;

namespace HCFramework.News
{
    public partial class NewsManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // An cac control chua dung den
                PanelAddAndEdit.Visible = false;
                PanelGrid.Visible = true;
                // Load category
                cboCategory.DataSource = new HCDataContext().NewsCategories.ToList();
                cboCategory.DataBind();
            }
        }
        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            LoadGridData();
        }
        public void LoadGridData()
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.News
                         orderby a.DateCreated descending
                         select new { a.NewsID, a.DisplayInLang, a.Title, a.NewsCategory.Name, a.User.Username, a.DateCreated };
            RadGridMain.DataSource = result;
            RadGridMain.DataBind();
        }
        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Them")
            {
                PanelAddAndEdit.Visible = true;
                PanelGrid.Visible = false;
                ButtonAdd.Visible = true;
                ButtonUpdate.Visible = false;
            }
            if (e.CommandName == "Xoa")
            {
                GridDataItem item = (GridDataItem)e.Item;
                C.NewsController.Delete(int.Parse(item["NewsID"].Text));
            }
            if (e.CommandName == "Sua")
            {
                GridDataItem item = (GridDataItem)e.Item;
                LabelID.Text = item["NewsID"].Text;
                BindEditForm();

                PanelAddAndEdit.Visible = true;
                PanelGrid.Visible = false;
                ButtonAdd.Visible = false;
                ButtonUpdate.Visible = true;
            }
        }
        private void BindEditForm()
        {
            int id = int.Parse(LabelID.Text);
            New n = C.NewsController.GetByID(id);
            cboLang.ClearSelection();
            cboLang.SelectedValue = n.DisplayInLang;
            txtTitle.Text = n.Title;
            txtSummary.Text = n.Summary;
            RadEditor1.Content = n.Content;
            cboCategory.ClearSelection();
            cboCategory.SelectedValue = n.CategoryID.ToString();
        }
        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            PanelAddAndEdit.Visible = false;
            PanelGrid.Visible = true;
        }
        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            // Insert a record
            New n = new New();
            n.DisplayInLang = cboLang.SelectedValue;
            n.Title = txtTitle.Text;
            n.Summary = txtSummary.Text;
            n.Content = RadEditor1.Content;
            n.CategoryID = int.Parse(cboCategory.SelectedValue);
            n.UserIdCreated = C.UserController.GetCurrentUser().UserID;
            C.NewsController.Insert(n);

            // Return to grid
            PanelAddAndEdit.Visible = false;
            PanelGrid.Visible = true;
        }
        protected void ButtonUpdate_Click(object sender, EventArgs e)
        {
            // Update
            int id = int.Parse(LabelID.Text);
            HCDataContext db = new HCDataContext();
            New n = C.NewsController.GetByID(id, db);
            n.DisplayInLang = cboLang.SelectedValue;
            n.Title = txtTitle.Text;
            n.Summary = txtSummary.Text;
            n.Content = RadEditor1.Content;
            n.CategoryID = int.Parse(cboCategory.SelectedValue);
            n.UserIdCreated = C.UserController.GetCurrentUser().UserID;
            C.NewsController.Update(n, db);

            // Return to grid
            PanelAddAndEdit.Visible = false;
            PanelGrid.Visible = true;
        }
    }
}